<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "yyh_menu".
 *
 * @property int $id
 * @property string $name 名称
 * @property int|null $parent_id 上级菜单
 * @property string $url URL
 * @property int|null $sort 排序
 */
class Menu extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'yyh_menu';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['name', 'url'], 'required'],
            [['parent_id', 'sort'], 'integer'],
            [['name', 'url'], 'string', 'max' => 32],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '名称',
            'parent_id' => '上级菜单',
            'url' => 'URL',
            'sort' => '排序',
        ];
    }

    //得到树形结构的病种数据
    public static function getTreeData($where = '', $checked_ids_string = '')
    {
        $res = [];
        $children = [];

        if (null != $where && $where == '') {
            $menu_objs = self::find()->orderBy('parent_id, sort asc, url, name')->asArray()->all();
        } else {
            $menu_objs = self::find()->where($where)->orderBy('parent_id, sort asc, url, name')->asArray()->all();
        }

        if (null != $checked_ids_string && $checked_ids_string != '') {
            $id_array = explode(",", $checked_ids_string);
            if (null != $id_array && count($id_array) > 0) {
                foreach ($id_array as $value_id) {
                    foreach ($menu_objs as $i => $d) {
                        if ((int)$d['id'] == (int)trim($value_id)) {
                            $menu_objs[$i]['checked'] = true;
                        }
                    }
                }
            }
        }

        foreach ($menu_objs as $d) {
            if ((int)$d['parent_id'] < 1 && (int)$d['parent_id'] != -1) {
                $res[] = $d;
            } else {
                $children[$d['parent_id']][] = $d;
            }
        }
        foreach ($res as $i => $r) {
            if (null != $children[$r['id']]) {
                $res[$i]['children'] = $children[$r['id']];
            }
        }

        return $res;
    }

    public static function getUserRootMenu($parent_id = 0)
    {
        $job_id = Yii::$app->user->identity->job;
        $job = Job::find()->where("id = {$job_id}")->one();
        
        if (null != $job) {
            if ('超级管理员' == $job->name) {
                return Menu::find()->select('name, url')->where("parent_id={$parent_id}")->orderBy('sort,id')->asArray()->all();
            } else {
                $menu_powers = trim($job->menu_powers);
                if ('' != $menu_powers) {
                    return Menu::find()->select('name, url')->where("parent_id={$parent_id} and id in ({$job->menu_powers})")->orderBy('sort,id')->asArray()->all();
                }
            }
        }

        return null;
    }
}
